﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using COFE.Core;
using COFE.Core.Validater;

namespace COFE.Web
{
    /// <summary>
    /// Represents a list of metadata in an Entry, or a list of PropertyPair of COFEEntry.
    /// </summary>
    public class MetadataList : List<PropertyPair>, ISupportXml
    {
        #region Constructor

        public MetadataList(IEnumerable<PropertyPair> items)
        {
            this.AddRange(items);
        }

        public MetadataList()
        {
        }


        #endregion

        #region Methods

        public PropertyPair Find(object property)
        {
            foreach (var meta in this)
                if (meta.Property.Equals(property))
                    return meta;
            return null;
        }

        /// <summary>
        /// Serialize the metadatalist to xml.
        /// </summary>
        /// <returns></returns>
        public XElement ToXml(bool indentLinks = false)
        {
            var entryXml = new XElement(XNames.Metadata);

            foreach (var meta in this)
            {
                string property = COFEShared.PropertyDictionary.GetPropertyString(meta.Property);
                entryXml.Add(new XElement(XName.Get(property, ServiceConstants.Namespace), meta.Value.ToString()));
            }

            return entryXml;
        }

        public static MetadataList FromXml(XElement itemXml)
        {
            var retVal = new MetadataList();

            foreach (var meta in itemXml.Elements())
            {
                if (meta.Name.LocalName != XNames.Link.LocalName)
                    try
                    {
                        object property = COFEShared.PropertyDictionary.GetProperty(meta.Name.LocalName);
                        string propertyString = meta.Value;
                        if (property != null)
                        {
                            var type = PropertyTypeExtension.GetDefaultPropertyType(property);
                            object value = null;
                            switch (type.ToString())
                            {
                                case "System.Int32":
                                    value = Int32.Parse(propertyString);
                                    break;
                                case "System.Int64":
                                    value = Int64.Parse(propertyString);
                                    break;
                                case "System.Boolean":
                                    value = Boolean.Parse(propertyString);
                                    break;
                                case "System.DateTime":
                                    value = DateTime.Parse(propertyString);
                                    break;
                                case "System.String":
                                    value = propertyString;
                                    break;
                                case "System.Guid":
                                    value = new Guid(propertyString);
                                    break;
                            }
                            if (value != null)
                                retVal.Add(new PropertyPair(property, value));

                        }

                    }
                    catch
                    {

                    }
            }
            return retVal;
        }


        #endregion

        #region Data

        #endregion

        #region Public Properties


        #endregion

    }
}
